var balance;
var amount;
var status;
var pwd;
var id;
    $(function(){
        $("#customerPhone").val("13124156345");
        $.ajax({
            url : "/parking/selectbyphone",
            type : "get",
            data : {
                "userPhone" : $("#customerPhone").val()
            },
            success : function(data) {
                balance=data.userBalance;
                amount=data.userAmount;
                status=data.userStatus;
                pwd=data.userPwd;
                id=data.userId;
            }
        });
        var oldPwdFlag;
        $("#customerOldPwd").blur(function(){
            var customerOldPwd = $("#customerOldPwd").val();
                    if (pwd==customerOldPwd) {
                        $("#oldPwdSpan").html(" *密码正确√").css("color", "green");
                        oldPwdFlag = 1;
                    } else {
                        $("#oldPwdSpan").html(" *密码输入错误").css("color", "red");
                        oldPwdFlag = 0;
                    }

        });
        var newPwdCheck = 0;
        $("#customerNewPwd")
            .blur(
                function() {
                    var customerNewPwd = $(this).val();
                    var passReg = /^(?=.*([a-zA-Z].*))(?=.*[0-9].*)[a-zA-Z0-9-*/+.~!@#$%^&*()]{6,20}$/;
                    var numberReg = /^[0-9]$/;
                    var stringReg = /^[A-Za-z]$/;
                    if (customerNewPwd == "") {
                        $("#newPwdSpan").html(" *密码不能为空").css("color", "red");
                        newPwdCheck = 0;
                    } else if (passReg.test(customerNewPwd)) {
                        $("#newPwdSpan").html(" *密码格式正确√").css("color","green");
                        newPwdCheck = 1;
                    } else {
                        if (customerNewPwd.length < 6) {
                            $("#newPwdSpan").html(" *密码长度不能小于6").css("color","red");
                            newPwdCheck = 0;
                        } else {
                            var j = 0; //用来计算是否密码全为数字
                            for (var i = 0; i < customerNewPwd.length; i++) {
                                var p1 = parseInt(customerNewPwd.charAt(i));
                                if (numberReg.test(p1)) {
                                    j++;
                                }
                                if (j == customerNewPwd.length) {
                                    $("#newPwdSpan").html(" *密码必须包含字母").css("color", "red");
                                    newPwdCheck = 0;
                                }
                            }
                            var k = 0; //用来计算是否密码全为字母
                            for (var i = 0; i < customerNewPwd.length; i++) {
                                var p1 = customerNewPwd.charAt(i);
                                if (stringReg.test(p1)) {
                                    k++;
                                }
                                if (k == customerNewPwd.length) {
                                    $("#newPwdSpan").html(" *密码必须包含数字").css("color", "red");
                                    newPwdCheck = 0;
                                }
                            }
                        }
                    }
                });
        var reNewPwdCheck = 0;
        $("#customerReNewPwd").blur(
            function() {
                var customerNewPwd = $("#customerNewPwd").val();
                var customerReNewPwd = $(this).val();
                if (customerReNewPwd == "") {
                    $("#reNewPwdSpan").html(" *密码不能为空").css("color", "red");
                    reNewPwdCheck = 0;
                } else if (customerNewPwd.length >= 6 && customerReNewPwd.length >= 6
                    && customerNewPwd == customerReNewPwd) {
                    $("#reNewPwdSpan").html(" *两次密码一致√").css("color", "green");
                    reNewPwdCheck = 1;
                } else {
                    $("#reNewPwdSpan").html(" *两次密码不一致").css("color", "red");
                    reNewPwdCheck = 0;
                }
            });
        $("#updatePwdBtn").click(function(){
            if(oldPwdFlag==1&&newPwdCheck==1&&reNewPwdCheck==1){
                var customerNewPwd = $("#customerNewPwd").val();
                $.ajax({
                    url : "/parking/userUpd",
                    type : "get",
                    dataType: "json",
                    data : {
                        "userId" : id,
                        "userPwd" : customerNewPwd,
                        "userBalance": balance,
                        "userAmount": amount,
                        "userStatus": status
                    },
                    success : function(data) {
                        if(data.updResult == "true"){
                            alert("修改成功");
                            window.location.href="http://localhost:9000/pwd_update.html";
                        }else{
                            alert("修改失败");
                            window.location.href="http://localhost:9000/pwd_update.html";
                        }
                    }
                });

            }else{
                alert("修改失败");
                window.location.href="http://localhost:9000/pwd_update.html";
                window.event.returnValue=false;
            }
        });

        $("#backInfoBtn").click(function(){
            window.location.href="http://localhost:9000/info_edit.html";
            window.event.returnValue=false;
        });
    });
